##Combining Variables
data = data %>% 
  mutate(race_treat_bin = ifelse(race_treatment == 'white',0,1) ) %>% 
  mutate(country_treat_bin = ifelse(country_or_world == 'country',0,1)) 

##Renaming variables
data = data %>% dplyr::rename("bot_check" = "IN1",
                              "bot_check_redo" = "IN2",
                              "birth_year" = "IN3",
                              'gender' = "IN4",
                              'zip_code' = "IN5",
                              'hispanic' = "ID1",
                              'race' = "ID2",
                              'demparty' = "DE4",
                              'thermom_black' = "ID3_1",
                              'thermom_white' = "ID3_2",
                              'thermom_poor' = "ID3_3",
                              'thermom_college' = "ID3_4",
                              'thermom_envactivist' = "ID3_5",
                              'thermom_othercountry' = "ID3_6",
                              'focus_us' = 'ID4_1',
                              'informed_global' = 'ID4_2',
                              'flood_damage' = 'CA1',
                              'extreme_heat' = 'CA2',
                              'gw_real' = 'CA3',
                              'gw_personal' = 'CA4',
                              'c_ushelp_ad' = 'CE4',
                              'c_president' = 'CE3',
                              'c_donate_100' = 'CE2',
                              'c_moral_ad' = 'CE7_1',
                              'c_toomuchtalk_ad' = 'CE7_2',
                              'c_moresimilar_ad' = 'CE7_3',
                              'c_worried_ad' = 'CE7_4',
                              'education' = 'DE1',
                              'income' = 'DE2',
                              'party' = 'DE3',
                              'party_other' = 'DE3_4_TEXT',
                              'party_lean' = 'DE4',
                              'travel' = 'DE5')

data = data %>% 
  mutate(black_treatment = ifelse(race_treatment == "black", 1, 0)) %>% 
  mutate(brazil_treatment = ifelse(location_treatment1 == "Campo Grande, Brazil" | location_treatment1 == "Duque de Caixas, Brazil", 1, 0)) %>%
  mutate(blame_treatment = ifelse(citizen_treat == 'blame', 1, 0)) %>%
  mutate(global_treatment = ifelse(citizen_treat == 'global', 1, 0)) %>%
  mutate(thermom_oc_index = ifelse(thermom_othercountry > 50, 1, 0))

data = data %>% 
  mutate(focus_us_5 = dplyr::recode(focus_us, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1, .default = NA_real_) )  %>%
  mutate(focus_us_rev_5 = dplyr::recode(focus_us, "Strongly agree" = 1, "Somewhat agree" = 2, "Neither agree nor disagree" = 3, "Somewhat disagree" = 4, "Strongly disagree" = 5, .default = NA_real_) )  %>% 
  mutate(informed_global_5 = dplyr::recode(informed_global, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1, .default = NA_real_) ) 

#Creating index - outward orientation
data = data %>% 
  mutate(focus_us_rev_z = (focus_us_rev_5-mean(focus_us_rev_5,na.rm=T))/sd(focus_us_rev_5,na.rm=T)) %>% 
  mutate(informed_global_z = (informed_global_5-mean(informed_global_5,na.rm=T))/sd(informed_global_5,na.rm=T)) %>% 
  mutate(c_out_orient_index = (focus_us_rev_z+informed_global_z)/2)
data$c_out_orient_index

#Recoding vars as 1-5
data = data %>% 
  mutate(c_president_6 = dplyr::recode(c_president,
                                       "No aid" = 1,
                                       "Between $0.5 - $1.5bn (the recent proposal)" = 2,
                                       "Between $2bn - $5bn" = 3,
                                       "Between $6bn - $10bn" = 4,
                                       "Between $11bn - $15bn (the original proposal)" = 5,
                                       "More than $15bn" = 6)) %>% 
  mutate(c_donate_100 = (as.numeric(gsub("\\$","", c_donate_100)))) %>% 
  mutate(c_donate_1 = (as.numeric(gsub("\\$","", c_donate_100)))/100) %>% 
  mutate(c_moral_5 = dplyr::recode(c_moral_ad, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1) ) %>% 
  mutate(c_ushelp_5 = dplyr::recode(c_ushelp_ad, "Strongly agree" = 5, "Agree" = 4, "Neither agree nor disagree" = 3, "Disagree" = 2, "Strongly disagree" = 1) ) %>% 
  mutate(c_toomuchtalk_5 = dplyr::recode(c_toomuchtalk_ad, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1) ) %>% 
  mutate(c_no_toomuchtalk_5 = dplyr::recode(c_toomuchtalk_ad, "Strongly agree" = 1, "Somewhat agree" = 2, "Neither agree nor disagree" = 3, "Somewhat disagree" = 4, "Strongly disagree" = 5) ) %>% 
  mutate(c_moresimilar_5 = dplyr::recode(c_moresimilar_ad, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1) ) %>% 
  mutate(c_worried_5 = dplyr::recode(c_worried_ad, "Strongly agree" = 5, "Somewhat agree" = 4, "Neither agree nor disagree" = 3, "Somewhat disagree" = 2, "Strongly disagree" = 1) )

#Creating index - global warming concern
data = data %>% 
  mutate(c_worried_z = (c_worried_5-mean(c_worried_5,na.rm=T))/sd(c_worried_5,na.rm=T)) %>% 
  mutate(c_no_toomuchtalk_z = (c_no_toomuchtalk_5-mean(c_no_toomuchtalk_5,na.rm=T))/sd(c_no_toomuchtalk_5,na.rm=T)) %>% 
  mutate(c_gw_concern_index = (c_worried_z+c_no_toomuchtalk_z)/2)

##Demographic and other cleaning
data = data %>% 
  mutate(race_simpl = recode(race, 
                             "White" = "White",
                             "Black or African American" = "Black", 
                             "Asian" = "Asian", 
                             "American Indian or Alaska Native" = "American Indian", 
                             "Other (please specify" = "Other", 
                             .default = "Mixed Race") ) %>% 
  mutate(no_travel = ifelse(travel == "No travel outside of the US", 1, 0)) %>% 
  mutate(travel_gs = if_else(grepl("South America|Africa", travel, ignore.case = TRUE), 1, 0)) %>%
  mutate(college = ifelse(education == "2-year college degree" | education == "4 year college degree" | education == "Postgraduate degree (MA, MBA JD, PhD, etc.)", 1, 0)) %>% 
  mutate(female = ifelse(gender == "Female", 1, 0)) %>% 
  mutate(income_scale =  dplyr::recode(income, 
                                       "Less than $20,000" = 1, 
                                       "$20,000-$29,999" = 2, 
                                       "$30,000-$39,999" = 3, 
                                       "$40,000-$49,999" = 4,
                                       "$50,000-$74,999" = 5, 
                                       "$75,000-$99,999" = 6, 
                                       "$100,000-$149,999" = 7, 
                                       "$150,000-$199,999" = 8, 
                                       "200,000-$249,999" = 9, 
                                       "$250,000 or more" = 10, 
                                       .default = NA_real_) )



data$party[is.na(data$party)] <- "Other"
data$party_lean[is.na(data$party_lean)] <- "Other"

#Assign values to party_final based on party and party_lean columns
data$party_final_analysis <- ifelse(data$party == "Democrat" | data$party_lean == "Democratic Party", "Democrat",
                                    ifelse(data$party == "Republican" | data$party_lean == "Republican Party", "Republican",
                                           "Other"))

data$party_final <- ifelse(data$party == "Democrat", "Democrat",
                           ifelse(data$party == "Republican", "Republican",
                                  ifelse(data$party == "Independent" & data$party_lean == "Democratic Party", "Democrat Lean",
                                         ifelse(data$party == "Independent" & data$party_lean == "Republican Party", "Republican Lean",
                                                "Other"))))

#Recode variables
data$frame <- factor(data$citizen_treat)
data$foreigntreat <- as.integer(factor(data$country_or_world))
data$racetreat <- as.integer(factor(data$race_treatment))
data$weather_shock <- ifelse(data$flood_damage == "Yes", 1, 0)
data$heat_shock <- ifelse(data$extreme_heat == "Yes, on 1 or 2 days", 1,
                          ifelse(data$extreme_heat == "Yes, on 3 to 9 days", 2,
                                 ifelse(data$extreme_heat == "Yes, on 10 or more days", 3, 0)))

data$education[data$education == "Did not graduate from high school"] <- 1
data$education[data$education == "High school graduate"] <- 1
data$education[data$education %in% c("2-year college degree", "Some college, but no degree")] <- 2
data$education[data$education == "4-year college degree"] <- 3
data$education[data$education == "Postgraduate degree (MA, MBA JD, PhD, etc.)"] <- 4
data$education <- as.integer(data$education)

data$white <- ifelse(data$race == "White", 1, 0)
data$black <- ifelse(data$race == "Black or African American", 1, 0)

data = data %>% 
  mutate(c_president_z = (c_president_6-mean(c_president_6,na.rm=T))/sd(c_president_6,na.rm=T)) %>% 
  mutate(c_ushelp_z = (c_ushelp_5-mean(c_ushelp_5,na.rm=T))/sd(c_ushelp_5,na.rm=T)) %>% 
  mutate(message_main = (c_president_z+c_ushelp_z)/2)
data$message_main

data$frame <- relevel(data$frame, ref = "none")
data$collective_guilt <- data$c_moral_5
data$social_prox <- data$c_moresimilar_5
data$worriedwarming <- data$c_gw_concern_index
data$president <- data$c_president_6
data$us_help <- data$c_ushelp_5

max(data$c_donate_100, na.rm = TRUE)
max(data$c_donate_1, na.rm = TRUE)

sg_data <- subset(data, select = c("message_main", "president","us_help", "c_donate_1", "collective_guilt", "social_prox", "worriedwarming", "c_worried_5","c_no_toomuchtalk_5"))
summary_table <- data.frame(
  Variable = c("Aid Index", "Presidential Aid (Aid)","US Gov Aid (Aid)","Personal Donation", "Collective Guilt", "Social Proximity", "GW Worry Index",'GW Worry (GW)', 'Talk about GW (GW)'),
  N = sapply(sg_data, function(x) sum(!is.na(x))),
  Mean = sapply(sg_data, mean, na.rm = TRUE),
  SD = sapply(sg_data, sd, na.rm = TRUE),
  Min = sapply(sg_data, min, na.rm = TRUE),
  Max = sapply(sg_data, max, na.rm = TRUE)
)
print(summary_table)
latex_table <- xtable(summary_table)

# Print the LaTeX table code
print(latex_table, include.rownames = FALSE)

# Identify variables to scale
vars_to_scale <- c("birth_year","thermom_black","thermom_white","thermom_poor",
                   "thermom_othercountry","education","thermom_oc_index", 'collective_guilt',
                   "focus_us_5","focus_us_rev_5","informed_global_5","c_out_orient_index","c_president_6","c_donate_1",
                   "c_toomuchtalk_5","c_no_toomuchtalk_5","c_moresimilar_5","c_worried_5","c_worried_z","c_no_toomuchtalk_z",
                   "c_gw_concern_index","no_travel","travel_gs","college","female","income_scale",
                   "foreigntreat","racetreat","weather_shock","heat_shock","white","black","message_main",
                   "social_prox","worriedwarming","president","us_help")

# Custom function to scale non-binary numeric variables, handling NAs
scale_if_not_binary <- function(x) {
  # Save the original NA positions
  na_positions <- is.na(x)
  
  # Remove NAs for the binary check
  x_non_na <- x[!na_positions]
  
  # Check if the variable is numeric and not binary
  if (is.numeric(x_non_na) && length(unique(x_non_na)) > 2) {
    # Scale the non-NA values
    x_scaled <- scale(x_non_na)
    
    # Create a vector to store the scaled values with NAs back in place
    x_final <- x
    x_final[!na_positions] <- x_scaled
    
    return(x_final)
  } else {
    return(x)  # Return the original variable if it's binary or not numeric
  }
}


# Apply the custom function to the selected variables
data[vars_to_scale] <- lapply(data[vars_to_scale], scale_if_not_binary)

data$frame <- relevel(data$frame, ref = "none")

dems <- subset(data, party_final_analysis == 'Democrat')
reps <- subset(data, party_final_analysis == 'Republican')
